// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import { g_bundleName } from "../mgr/assetsManager";
import SoundMgr from "../mgr/soundManager";



const { ccclass, property } = cc._decorator;

//管理类基类 挂在主节点上 用于管理各种调度问题

@ccclass
export default class ToggleButton extends cc.Component {

    @property(cc.Node)
    default: cc.Node = null;


    onLoad() {



        for (let i in this.node.children) {
            const node = this.node.children[i];
            node.on('click', this._MenuBtnCallBack, this);
        }


    }


    protected start(): void {
        this.refreshBtns(this.default);
        // this.scheduleOnce(() => {
        cc.systemEvent.emit('toggleButtonCallBack', this.default.name);
        // }, 0.02);

    }



    refreshBtns(node: cc.Node) {
        for (let i in this.node.children) {
            this.node.children[i].getComponent(cc.Button).interactable = true;
            if (this.node.children[i].name == node.name) {
                this.node.children[i].getComponent(cc.Button).interactable = false;
            }
        }
    }

    _MenuBtnCallBack(target) {
        console.log('target = ', target.node.name);
        SoundMgr.Ins().playEffect('btn_touch', g_bundleName.resources);
        cc.systemEvent.emit('toggleButtonCallBack', target.node.name);
        this.refreshBtns(target.node)
    }



}
